From e143ca7cff8a3271a45b4680fb1ce847251f6018 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sat, 23 Jan 2021 16:13:51 -0600 Subject: [PATCH] Add a setting for uploaded file format --- src/pgwui_upload_core/check_settings.py | 13 ++++++++++++- src/pgwui_upload_core/exceptions.py | 9 ++++++++- src/pgwui_upload_core/views/upload.py | 13 ++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/pgwui_upload_core/check_settings.py b/src/pgwui_upload_core/check_settings.py index 04eb1a2..60c5118 100644 --- a/src/pgwui_upload_core/check_settings.py +++ b/src/pgwui_upload_core/check_settings.py @@ -1,4 +1,4 @@ -# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/ +# Copyright (C) 2020, 2021 The Meme Factory, Inc. http://www.karlpinc.com/ # This file is part of PGWUI_Upload_Core. # @@ -20,12 +20,14 @@ # Karl O. Pinc from pgwui_common import checkset +from pgwui_upload_core import exceptions as upload_core_ex UPLOAD_SETTINGS = ['menu_label', 'literal_column_headings', 'trim', 'null', + 'file_format' ] REQUIRED_SETTINGS = [] BOOLEAN_SETTINGS = [] @@ -34,6 +36,15 @@ BOOLEAN_CHOICE_SETTINGS = ['literal_column_headings', 'null'] +def validate_file_format(component, errors, settings): + '''Make sure the values are those allowed + ''' + value = settings.get('file_format') + if value not in ('csv', 'tab'): + errors.append( + upload_core_ex.BadFileFormatError(component, value)) + + def check_settings( component, all_setngs, required_setngs, boolean_setngs, boolean_choice_setngs, component_config): diff --git a/src/pgwui_upload_core/exceptions.py b/src/pgwui_upload_core/exceptions.py index 662b7f6..58c8e7d 100644 --- a/src/pgwui_upload_core/exceptions.py +++ b/src/pgwui_upload_core/exceptions.py @@ -1,4 +1,4 @@ -# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/ +# Copyright (C) 2020, 2021 The Meme Factory, Inc. http://www.karlpinc.com/ # This file is part of PGWUI_Upload. # @@ -28,6 +28,13 @@ class UploadError(core_ex.Error): pass +class BadFileFormatError(UploadError): + def __init__(self, component, value): + super().__init__( + f'The "pgwui:{component}:file_format" PGWUI setting is ({value}) ' + 'it must be either "csv" or "tab" or the setting be omitted') + + class NoTableError(UploadError): '''No table uploaded''' def __init__(self, e, descr='', detail=''): diff --git a/src/pgwui_upload_core/views/upload.py b/src/pgwui_upload_core/views/upload.py index 7dbab4e..7c90a33 100644 --- a/src/pgwui_upload_core/views/upload.py +++ b/src/pgwui_upload_core/views/upload.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015, 2018, 2020 The Meme Factory, Inc. +# Copyright (C) 2015, 2018, 2020, 2021 The Meme Factory, Inc. # http://www.karlpinc.com/ # This file is part of PGWUI_Upload_Core. @@ -38,6 +38,10 @@ from pgwui_core.core import ( UploadData, doublequote, ) +from pgwui_core.constants import ( + CSV, + TAB, +) from pgwui_upload_core import exceptions as upload_ex @@ -62,9 +66,16 @@ class UploadCoreInitialPost(UploadNullFileInitialPost): # otherwise the result is never displayed on the page. return (val == 'yes-always' or val == 'choice-yes') + def find_upload_fmt(self, upload_settings): + if upload_settings['file_format'] == 'csv': + return CSV + else: + return TAB + def build(self, settings={}): super().build(settings) upload_settings = settings['pgwui'][self.component] + self.upload_fmt = self.find_upload_fmt(upload_settings) self.upload_null = self.boolean_choice(upload_settings, 'null') self.trim_upload = self.boolean_choice(upload_settings, 'trim') self.literal_col_headings = self.boolean_choice( -- 2.34.1